namespace PickGold.Charting
{
    using System;
    using System.Collections;
    using System.Reflection;

    [SRDescription("DescriptionAttributeAxisScaleSegmentCollection_AxisScaleSegmentCollection")]
    internal class AxisScaleSegmentCollection : CollectionBase
    {
        private Axis _axis;
        private AxisScaleSegment _enforcedSegment;
        internal bool AllowOutOfScaleValues;

        public AxisScaleSegmentCollection()
        {
        }

        internal AxisScaleSegmentCollection(Axis axis)
        {
            this._axis = axis;
        }

        public int Add(AxisScaleSegment segment)
        {
            return base.List.Add(segment);
        }

        internal void EnforceSegment(AxisScaleSegment segment)
        {
            this._enforcedSegment = segment;
        }

        public AxisScaleSegment FindScaleSegmentForAxisValue(double axisValue)
        {
            if (base.List.Count != 0)
            {
                if (this._enforcedSegment != null)
                {
                    return this._enforcedSegment;
                }
                for (int i = 0; i < base.Count; i++)
                {
                    if (axisValue < this[i].ScaleMinimum)
                    {
                        if (i == 0)
                        {
                            return this[i];
                        }
                        if (Math.Abs((double) (this[i].ScaleMinimum - axisValue)) < Math.Abs((double) (axisValue - this[i - 1].ScaleMaximum)))
                        {
                            return this[i];
                        }
                        return this[i - 1];
                    }
                    if (axisValue <= this[i].ScaleMaximum)
                    {
                        return this[i];
                    }
                    if (i == (base.Count - 1))
                    {
                        return this[i];
                    }
                }
            }
            return null;
        }

        protected override void OnInsertComplete(int index, object value)
        {
            ((AxisScaleSegment) value).axis = this._axis;
        }

        protected override void OnSetComplete(int index, object oldValue, object newValue)
        {
            ((AxisScaleSegment) newValue).axis = this._axis;
        }

        [SRDescription("DescriptionAttributeAxisScaleSegmentCollection_Item")]
        public AxisScaleSegment this[int index]
        {
            get
            {
                return (AxisScaleSegment) base.List[index];
            }
        }
    }
}

